#include "networkmgr.h"
#include "NetWorkerDef.h"

NetWorkMgr::NetWorkMgr()
{

}
NetWorkMgr::~NetWorkMgr()
{
    qDebug() << "Destructor" << this;
}


NETWORKMGR_EXPORT NetWorkMgr& NetWorkMgr::instance()
{
    static NetWorkMgr networkmgr;
    return networkmgr;
}

void NetWorkMgr::Login(const QJsonObject & jsParam, const QString & strTask)
{
    qDebug() << "Login:";
    //组合
    //获取ip port
    QString strurl;
    QString strusername;
    QString strpassword;
    QString strip;
    QString strport;

    if(jsParam.contains("ip") &&
        jsParam.contains("port") &&
        jsParam.contains("username") &&
        jsParam.contains("password"))
    {
        strip = jsParam["ip"].toString();
        strport = jsParam["port"].toString();
        strusername = jsParam["username"].toString();
        strpassword = jsParam["password"].toString();
    }

    initNetwork(strip, strport);

    //密码转为MD5
    QByteArray hash = QCryptographicHash::hash(strpassword.toUtf8(), QCryptographicHash::Md5);
    strpassword = hash.toHex();


    strurl = "http://";
    strurl += strip;
    strurl += ":";
    strurl += strport;
    strurl += url_def::login;

    qDebug() << "请求:" <<strurl;

    QJsonObject jsreq;
    jsreq["username"] = strusername;
    jsreq["password"] = strpassword;

    HttpWorker *worker = new HttpWorker(QUrl(strurl), jsreq);

    QObject::connect(worker, &HttpWorker::requestLoginFinished, this, &NetWorkMgr::requestLoginFinished);

    pool.start(worker);

}
void NetWorkMgr::requestLoginFinished(QByteArray responseData)
{
    qDebug() << "requestLoginFinished:" << responseData.toStdString();
    //解析，判断是否成功，发送信号

    QJsonParseError error;
    QJsonDocument doc = QJsonDocument::fromJson(responseData, &error);

    if (error.error != QJsonParseError::NoError)
    {
        qDebug() << "失败";
        return;
    }
    QJsonObject jsresult = doc.object();

    if(jsresult.contains("code"))
    {
        int ncode = jsresult["code"].toInt();
        if(ncode == 200)
        {
            jsresult["bsuccess"] = true;
            emit LoginDone(jsresult);
            qDebug() << "解析结果:" << jsresult["code"].toString();
        }
        else
        {
            jsresult["bsuccess"] = false;
            emit LoginDone(jsresult);
            qDebug() << "解析结果:" << jsresult["code"].toString();
        }
    }

}

void NetWorkMgr::GetPerosnInfoList(const QJsonObject & jsParam, const QString & strTask)
{
    qDebug() << "GetPerosnInfoList:" << m_strIp;

    QString strurl;
    strurl += "http://";
    strurl += m_strIp;
    strurl += ":";
    strurl += m_strPort;
    strurl += url_def::getpersoninfolist;

    qDebug() << "请求:" <<strurl;

    HttpWorker *worker = new HttpWorker(QUrl(strurl), jsParam);

    QObject::connect(worker, &HttpWorker::requestGetPersonInfoListFinished, this, &NetWorkMgr::requestGetPersonInfoListFinished);

    pool.start(worker);
}

void NetWorkMgr::requestGetPersonInfoListFinished(QByteArray responseData)
{
    qDebug() << "requestLoginFinished:" << responseData.toStdString();
    //解析，判断是否成功，发送信号

    QJsonParseError error;
    QJsonDocument doc = QJsonDocument::fromJson(responseData, &error);

    if (error.error != QJsonParseError::NoError)
    {
        qDebug() << "失败";
        return;
    }
    QJsonObject jsresult = doc.object();

    if(jsresult.contains("code"))
    {
        int ncode = jsresult["code"].toInt();
        if(ncode == 200)
        {
            jsresult["bsuccess"] = true;
            emit GetPersonInfoListDone(jsresult);
            qDebug() << "解析结果:" << jsresult["code"].toString();
        }
        else
        {
            jsresult["bsuccess"] = false;
            emit GetPersonInfoListDone(jsresult);
            qDebug() << "解析结果:" << jsresult["code"].toString();
        }
    }
}

void NetWorkMgr::initNetwork(const QString & strIp, const QString & strPort)
{
    m_strIp = strIp;
    m_strPort = strPort;
}
